12. Enable Location Tracking

L4 A11 Enable Location Tracking

Reference Documentation

Users often use Google Maps to see their current location. To display the device location on your map, you can use the location-data layer.

The location-data layer adds a My Location button to the top-right side of the map. When the user taps the button, the map centers on the device's location. The location is shown as a blue dot if the device is stationary, and as a blue chevron if the device is moving.

In this task, you enable the location-data layer.

Enabling location tracking in Google Maps requires a single line of code. However, you must make sure that the user has granted location permissions (using the runtime-permission model).

In this step, you request location permissions and enable the location tracking.

  1. In the AndroidManifest.xml file, verify that the FINE_LOCATION permission is already present. Android Studio inserted this permission when you selected the Google Maps template.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  1. In MapsActivity.kt, create a REQUEST_LOCATION_PERMISSION variable.
private val REQUEST_LOCATION_PERMISSION = 1
  1. To check if permissions are granted, create a method in the MapsActivity.kt called isPermissionGranted(). In this method, check if the user has granted the permission.
private fun isPermissionGranted() : Boolean {
  return ContextCompat.checkSelfPermission(
       this,
      Manifest.permission.ACCESS_FINE_LOCATION) === PackageManager.PERMISSION_GRANTED
}
  1. To enable location tracking in your app, create a method in the MapsActivity.kt called enableMyLocation() that takes no arguments and doesn't return anything.
    Check for the ACCESS_FINE_LOCATION permission. If the permission is granted, enable the location layer. Otherwise, request the permission:
private fun enableMyLocation() {
   if (isPermissionGranted()) {
       map.setMyLocationEnabled(true)
   }
   else {
       ActivityCompat.requestPermissions(
           this,
           arrayOf<String>(Manifest.permission.ACCESS_FINE_LOCATION),
           REQUEST_LOCATION_PERMISSION
       )
   }
}
  1. Call enableMyLocation() from the onMapReady() callback to enable the location layer.
override fun onMapReady(googleMap: GoogleMap) {
   …
   enableMyLocation()
}
  1. Override the onRequestPermissionsResult() method. If the requestCode is equal to REQUEST_LOCATION_PERMISSION permission is granted, and if the grantResults array is non empty with PackageManager.PERMISSION_GRANTED in its first slot, call enableMyLocation():
override fun onRequestPermissionsResult(
   requestCode: Int,
   permissions: Array<String>,
   grantResults: IntArray) {
   // Check if location permissions are granted and if so enable the
   // location data layer.
   if (requestCode == REQUEST_LOCATION_PERMISSION) {
       if (grantResults.size > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
           enableMyLocation()
       }
   }
}
  1. Run the app. There should be a pop up requesting access to the device's location. Allow the permission.

Note that this pop up may look different if you are not running the recommended API 29.

The map will now display the device's current location using a blue dot. Notice that there is a location button on the top right. If you move the map away from your location and click on this button it will center the map back to the device location.